use "ChoiKarivMullerSilverman_withTMI.dta", clear
cap drop epsilon_lb epsilon_ub

preserve
	import delimited "FDI-EU-Matlab/epsilons.csv", clear
	rename (var1 var2 var3) (id_member epsilon_lb epsilon_ub)
	tempfile tomerge1
	save `tomerge1'
restore


merge 1:1 id_member using `tomerge1', assert(match) nogen


gen tmi_eunondiff =  epsilon_ub

save "ChoiKarivMullerSilverman_withTMI.dta", replace

import delimited "EMAX-Matlab/emax.csv", clear 
save "emax.dta", replace
use "ChoiKarivMullerSilverman_withTMI.dta", clear
merge 1:1 id_member using "emax.dta", nogen
save "ChoiKarivMullerSilverman_withTMI.dta",replace


//put together random_emax_A, B C if needed
import delimited "FDI-EU-Matlab/epsilons_random_A.csv", clear
save "epsilons_random_A.dta", replace
import delimited "FDI-EU-Matlab/epsilons_random_B.csv", clear
save "epsilons_random_B.dta", replace
clear all
append using epsilons_random_A.dta epsilons_random_B.dta
save "epsilons_random.dta",replace
clear all
import delimited "FDI-EU-Matlab/epsilons_random_C.csv", clear
save "epsilons_random_C.dta", replace
clear all
append using epsilons_random.dta epsilons_random_C.dta
rename (var1 var2 var3) (id_member epsilon_lb epsilon_ub)
save "epsilons_random.dta",replace



use "ChoiKarivMullerSilverman_randomChoices.dta", clear
cap drop epsilon_lb epsilon_ub
merge 1:1 id_member using epsilons_random.dta, assert(match) nogen

/* if instead want to run entire Matlab with entire Bronars data at once with an output file called epsilons_random.csv....
preserve
	import delimited "FDI-EU-Matlab/epsilons_random.csv", clear
	rename (var1 var2 var3) (id_member epsilon_lb epsilon_ub)
	tempfile tomerge2
	save `tomerge2'
restore
merge 1:1 id_member using `tomerge2', assert(match) nogen

*/




gen tmi_eunondiff_placebo =  epsilon_ub

save "ChoiKarivMullerSilverman_randomChoices.dta", replace



import delimited "EMAX-Matlab/random_emax.csv", clear 
save "random_emax.dta", replace

use "ChoiKarivMullerSilverman_randomChoices.dta", clear
merge 1:1 id_member using "random_emax.dta", nogen
rename emax emax_placebo
save "ChoiKarivMullerSilverman_randomChoices.dta",replace





//combine to make final dataset
clear all

append using ChoiKarivMullerSilverman_randomChoices.dta ChoiKarivMullerSilverman_withTMI.dta


keep id_member emax emax_placebo tmi_nondiff tmi_nondiff_placebo tmi_combined_nondiff tmi_combined_nondiff_placebo tmi_eunondiff tmi_eunondiff_placebo placebo actual


save "ChoiKarivMullerSilverman_actualandrandom.dta", replace
